﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PropertyManagment.config;
using PropertyManagment.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.mapper.impl {

    public class SysRoleMapperImpl : BaseMapperImpl<SysRole>, SysRoleMapper {

        private readonly DbSet<SysUserRole> sysUserRoles;

        public SysRoleMapperImpl(AppDbContext context) : base(context) {
            sysUserRoles = context.Set<SysUserRole>();
        }

        public List<SysRole> getByUserId(long userId) {

            var query = from r in dbSet
                         join ur in sysUserRoles on r.id equals ur.role_id
                         where ur.user_id == userId
                         select new SysRole {
                             id = r.id,
                             name = r.name,
                             code = r.code,
                             remark = r.remark,
                             created = r.created,
                             updated = r.updated,
                             statu = r.statu
                         };
            List<SysRole> roles = query.ToList();

            return roles;
        }
    }
}
